package com.snow.useviewdemo.function.copy;

import android.content.Context;
import android.widget.TextView;

import com.snow.useviewdemo.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-08-29
 * desc   : textView的长按复制管理类
 */
public class SelectableTextManager {
    private static SelectableTextManager textManager;

    private HashMap<Integer, SelectableTextHelper> copyHelpers = new HashMap<>();
    //操作的内容
    private List<String> optionList;

    public static SelectableTextManager init() {
        if (textManager == null) {
            synchronized (SelectableTextManager.class) {
                if (textManager == null) {
                    textManager = new SelectableTextManager();
                }
            }
        }
        return textManager;
    }

    private SelectableTextManager() {
        optionList = new ArrayList<>();
    }

    /**
     * 隐藏所有显示
     */
    public void hindAll() {
        Iterator<Map.Entry<Integer, SelectableTextHelper>> iterator = copyHelpers.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, SelectableTextHelper> entry = iterator.next();
            entry.getValue().clearAll();
        }
    }

    /**
     * 将要操作复制的TextView注入
     *
     * @param textView
     */
    public void addTextCopy(TextView textView) {
        addTextCopy(textView, R.color.tt_blue, R.color.tt_blue, 20);
    }

    /**
     * 将要操作复制的TextView注入===设置参数
     *
     * @param textView
     * @param selectColor R.color.tt_blue
     * @param cursorColor R.color.tt_blue
     * @param cursorSize  20
     */
    public void addTextCopy(TextView textView, int selectColor, int cursorColor, int cursorSize) {
        Context mContext = textView.getContext();
        SelectableTextHelper selectableTextHelper = new SelectableTextHelper();
        selectableTextHelper.setHideAllListener(() -> hindAll());

        selectableTextHelper.setOptionList(optionList);
        selectableTextHelper.builder(new SelectableTextHelper.Builder(textView)
                .setSelectedColor(mContext.getResources().getColor(selectColor))
                .setCursorHandleSizeInDp(cursorSize)
                .setCursorHandleColor(mContext.getResources().getColor(cursorColor)));
        copyHelpers.put(textView.getId(), selectableTextHelper);
        selectableTextHelper.setSelectListener(new OnSelectListener() {
            @Override
            public void onTextSelected(String optionResult) {
                if (optionListener != null) {
                    optionListener.optionAction(optionResult);
                }
            }
        });
    }

    public void setSelectOptionListener(SelectOptionListener optionListener) {
        this.optionListener = optionListener;
    }

    private SelectOptionListener optionListener;

    public interface SelectOptionListener {
        void optionAction(String optionResult);
    }

    /**
     * 重置操作数据====必须放在调用addTextCopy方法之前
     *
     * @param strings
     */
    public void setOptionData(String[] strings) {
        optionList.clear();
        if (strings != null) {
            optionList.addAll(Arrays.asList(strings));
        }
    }
    /**
     * 添加操作数据====必须放在调用addTextCopy方法之前
     *
     * @param optionStr
     */
    public void addOptionData(String optionStr) {
        if (optionList != null) {
            optionList.add(optionStr);
        }
    }
}
